package com.vf.admin.client.controller;

import com.vf.admin.client.common.ClientBaseController;
import com.vf.admin.client.common.ClientConstants;
import com.vf.admin.client.common.RedisKeys;
import com.vf.admin.client.dto.FenSiTuanDto;
import com.vf.admin.client.service.AppFansClubService;
import com.vf.admin.client.service.AppRoomService;
import com.vf.admin.client.service.AppSetupService;
import com.vf.admin.client.service.AppUserService;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppFansClub;
import com.vf.admin.miaodong.entity.AppRoom;
import com.vf.admin.miaodong.entity.AppSetup;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.utils.StringEx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @author anxiang
 * @version 1.0.0
 * @ClassName AppFenSiTuanController.java
 * @Description 粉丝团管理
 * @createTime 2021年07月29日 12:48:00
 */
@Controller
@RequestMapping("/appfensituan")
public class AppFenSiTuanController extends ClientBaseController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    AppFansClubService appFansClubService;
    @Autowired
    AppUserService appUserService;
    @Autowired
    AppRoomService appRoomService;
    @Autowired
    AppSetupService appSetupService;
    @Autowired
    RedisUtil redisUtil;

    /**
     * @Description 房间获取粉丝团列表
     * @Param: [roomid 房间id, pageno 页数]
     * @return: com.vf.admin.common.entity.Result
     * @Author: ljx
     * @Date: 2021/07/29
     */
    @ResponseBody
    @RequestMapping("/list")
    public Result list(Integer roomid, Integer pageno) {
        try {
            if (roomid == null) {
                return this.errorJSON("房间id为空");
            }
            if (pageno == null) {
                return this.errorJSON("页码为空");
            }
            //获取最新数据
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);
            //获取房间信息
            AppRoom appRoom = appRoomService.getById(roomid);
            if (appRoom == null) {
                return this.errorJSON("房间不存在");
            }
            //添加分页数据
            SplitPageBean splitPageBean = new SplitPageBean(pageno, 20);
            List<AppFansClub> appFansClubList = appFansClubService.roomList(roomid, splitPageBean);
            //返回数据
            List<FenSiTuanDto> fenSiTuanDtoList = new ArrayList<>();
            for (AppFansClub appFansClub : appFansClubList) {
                AppUser user1=appUserService.get(appFansClub.getUserid());
                FenSiTuanDto fenSiTuanDto = new FenSiTuanDto();
                fenSiTuanDto.setId(appFansClub.getId());
                fenSiTuanDto.setRoomid(appFansClub.getRoomid());
                //用户头像
                fenSiTuanDto.setPic(appFansClub.getPic());
                //用户昵称
                fenSiTuanDto.setNick(appFansClub.getNick());
                //用户手机号
                fenSiTuanDto.setPhone(appFansClub.getPhone());
                fenSiTuanDto.setSex(user1.getSex());
                fenSiTuanDto.setMysign(user1.getMysign());
                fenSiTuanDto.setCreatetime(appFansClub.getCreatetime());
                fenSiTuanDtoList.add(fenSiTuanDto);
            }
            Map<String,Object> toMap=new HashMap<>(16);
            toMap.put("list",fenSiTuanDtoList);
            toMap.put("total",splitPageBean.getTotalCount());
            return this.sucessPage(toMap, splitPageBean.getNextPageNo());
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("房间获取粉丝团列表异常:", e);
            return this.errorJSON("系统繁忙，请稍后重试");
        }
    }

    /**
     * @Description 获取粉丝团说明
     * @Param: []
     * @return: com.vf.admin.common.entity.Result
     * @Author: ljx
     * @Date: 2021/07/29
     */
    @ResponseBody
    @RequestMapping("/shuoming")
    public Result shuoming() {
        try {
            //获取活动规则
            AppSetup setup = appSetupService.get();
            if (StringEx.isNotNull(setup.getFensituanshuoming())) {
                return this.sucessJSON(setup.getFensituanshuoming(), "获取成功");
            }
            return this.sucessJSON("", "获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取粉丝团说明异常:", e);
            return this.errorJSON("系统繁忙，请稍后重试");
        }
    }


}